package com.example.mvc.controller;

import com.example.mvc.bean.Book;
import com.example.mvc.bean.BookType;
import com.example.mvc.bean.User;
import com.example.mvc.service.BookService;
import com.example.mvc.util.Utils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/bookListServlet")
public class BookListServlet extends HttpServlet {
    BookService bookService= new BookService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String role = req.getParameter("role");

        List<Book> bookList = bookService.getBookList();
        HttpSession session = req.getSession();
        if (Utils.isEmpty(role)){
            User user = (User) session.getAttribute("user");
            int status = user.getStatus();
            role=status+"";
        }
        session.setAttribute("bookList",bookList);

        //获取书的类型列表为了选择书下拉框准备
        List<BookType> bookTypeList = bookService.getBookTypeList();
        //bookTypeList存入session中
        session.setAttribute("bookTypeList",bookTypeList);

        String contextPath = req.getContextPath();
        //role 1为普通用户  ，2为管理员
        if ("1".equals(role)){
            resp.sendRedirect(contextPath+"/user_index.jsp");
        }else {//调到管理员界面
            resp.sendRedirect(contextPath+"/admin_index.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }

}
